<?php
namespace GRH\Model\Table;

use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;

use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Expression;
use Zend\Db\Sql\Select;

use GRH\Model\Semaine;

class SemaineTable extends AbstractTableGateway
{
    protected $table = 'grh_semaine';
    
    /**
     * Constructeur
     * 
     * @param \Zend\Db\Adapter\Adapter $adapter
     */
    public function __construct(Adapter $adapter)
    {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new Semaine());
        $this->initialize();
    }
    
    /**
     * Récupérer toutes les semaines
     * 
     * @return Collection
     */
    public function getAll()
    {
        return $this->select();
    }
    
    /**
     * Récupérer toutes les semaines pour une utilisation dans une zone de liste
     * 
     * @return array
     */
    public function getSelectList()
    {
        $rows = $this->select();
        $arr = array();
        foreach($rows as $semaine)
            $arr[$semaine->semaine_id] = $semaine->libelle;
        
        return $arr;
    }
    
    /**
     * Récupérer une semaine
     * 
     * @param type $id
     * @return type
     * @throws \Exception
     */
    public function get($id)
    {
        $id = (int)$id;
        $row = $this->select(array('semaine_id' => $id))
                ->current();
        if(!$row)
            throw new \Exception("SemaineTable::get => Impossible de trouver '$id'");
        return $row;
    }
    
    public function getByArray($ids)
    {
        $rows = $this->select(function(Select $select) use($ids){
            $where = new Where();
            $where->in('semaine_id', $ids);
        });
        
        return $rows;
    }
    
    /**
     * Sauvegarder une semaine
     * 
     * @param \GRH\Model\Semaine $sem
     * @return type
     * @throws \Exception
     */
    public function save(Semaine $sem)
    {
        $data = array(
            'libelle' => $sem->libelle,
            'lundi' => $sem->lundi,
            'mardi' => $sem->mardi,
            'mercredi' => $sem->mercredi,
            'jeudi' => $sem->jeudi,
            'vendredi' => $sem->vendredi,
            'samedi' => $sem->samedi,
            'dimanche' => $sem->dimanche,
        );
        $id = (int)$sem->semaine_id;
        if($id == 0)
            $this->insert ($data);
        else
            if($this->get($id))
                $this->update ($data, array('semaine_id' => $id));
            else
                throw new \Exception("SemaineTable::save => Impossible de trouver '$id'");
        
        if($id == 0) return $this->lastInsertValue;
        else return $id;
    }
    
    public function delete($id)
    {
        $this->delete(array('semaine_id' => $id));
    }
}